/**
 * gets the selected grid rows numbers string
 *
 * param string field - field id
*/
function GetSelectedRowsString(field, grid_name)
{
	var checks = document.getElementsByTagName("INPUT");
	var result = '';
	for (i = 0; i < checks.length; i++)
	{
		if((checks[i].id == field) && (checks[i].checked) && (checks[i].parentNode.parentNode.id.match(grid_name)))
		{
			row_number = checks[i].parentNode.parentNode.id.replace(grid_name + '_row_', '');
			result = result + ',' + row_number;
		}
	}
	return result.substr(1);
}

/**
 * Filters grid's content
 * param object object - input field
*/
//TODO: to GREATLY optimize!
function LiveFilter(object, grid_name)
{
	//row with filters
	filter_row = object.parentNode.parentNode;
	
	//all  table cells		
	tds = document.getElementsByTagName('td');

	//displays all grid cells
	for (i = 0; i < tds.length; i++)
	{
		if (tds[i].id) 
		{
			if (tds[i].parentNode.id.match(grid_name + '_')) tds[i].parentNode.style.display = '';
		}
	}
	
	for (i = 0; i < tds.length; i++)
	{	
		if (tds[i].id)
		{
			id = tds[i].id.split('-');
			
			for (j = 0; j < filter_row.childNodes.length; j++)
			{
				if ((filter_row.childNodes[j].firstChild)&&(filter_row.childNodes[j].firstChild.tagName == 'INPUT'))
				{
					value = filter_row.childNodes[j].firstChild.value;
					name = filter_row.childNodes[j].firstChild.name;
					
					if (id[0] == name)
					{	
						//if we deal with url
						if (tds[i].firstChild.firstChild)
						{
							if (!tds[i].firstChild.firstChild.nodeValue.toLowerCase().match(value.toLowerCase()))
							{
								tds[i].parentNode.style.display = 'none';
							}
						}
						//if we deal with the text field
						else if (tds[i].firstChild)
						{
							//alert(tds[i].firstChild.nodeValue);
							if (!tds[i].firstChild.nodeValue.toLowerCase().match(value.toLowerCase()))
							{
								tds[i].parentNode.style.display = 'none';
							}				
						}
					}
				}
			}
		}
	}
}

/**
 * Clears live filter
 * param object object clicked object
*/
function ClearLiveFilter(object, grid_name)
{
	filter_row = object.parentNode.parentNode;
	for (j = 0; j < filter_row.childNodes.length; j++)
	{
		if (filter_row.childNodes[j].firstChild) filter_row.childNodes[j].firstChild.value = '';
	}
	LiveFilter(filter_row.firstChild.firstChild, grid_name);
}

/**
 * Marks a grid row
 * param integer row_number number of the row
 * param string key pressed key (optional)
*/
function MarkGridRow(row_number, key, grid_name)
{
	//gets current row element
	row = document.getElementById(grid_name + '_row_' + row_number);
	
	if (row == null) return;
	
	//if the row is hidden
	if (row.style.display == 'none') 
	{
		if (key == 'up')
		{ 
			MarkGridRow(Number(row_number)-1, key, grid_name);
		}
		else
		{ 
			if (key == 'down') MarkGridRow(Number(row_number)+1, key, grid_name);
		}
	}
	else
	{
		//if the row is not selected
		if (row.name != 'selectedRow')
		{
			//all rows on the page
			rows = document.getElementsByTagName('tr');
			for (i=0; i<rows.length; i++)
			{
				//if the row is selected
				if (rows[i].name == 'selectedRow')
				{
					rows[i].className = 'gridRow';
					rows[i].name = '';
					break;
				}
			}
			row.className = 'gridRowSelected';
			row.name = 'selectedRow';
		}
		else
		{
			row.className = 'gridRow';
			row.name = '';
		}
	}
}

/**
 * Selects a grid row
 * param string url_id id of the link element
*/
function SelectGridRow(url_id)
{
	if (document.getElementById(url_id))
	{
		window.location.replace(document.getElementById(url_id).href);
	}
}

/**
 * gets the number of the selected row
*/
function GetSelectedGridRowNumber(grid_name)
{
	//all rows on the page
	rows = document.getElementsByTagName('tr');
	
	for (i=0; i<rows.length; i++)
	{
		//if the row is selected
		if ((rows[i].name == 'selectedRow')&&(rows[i].id.match(grid_name)))
		{
			selected_row_id = rows[i].id
			break;
		}
	}
	row_number = selected_row_id.replace('row_', '');
	row_number = row_number.replace(grid_name+'_', '');
	return row_number;	
}

/**
 * Selects a grid row after a certain key buttons pressed
 * param string default_action deafault grid action
*/
function SelectGridRowByKeyPress(default_action, grid_name)
{
	row_number = GetSelectedGridRowNumber(grid_name);
	url_id = grid_name + '_' + default_action + '_' + row_number
	
	//selects a row
	SelectGridRow(url_id)
}

/**
 * Moves between grid rows
 * param string key pressed key
*/
function ShiftGridRowByKeyPress(key, grid_name)
{
	row_number = GetSelectedGridRowNumber(grid_name);
	
	switch(key)
	{
		case 'up':
			MarkGridRow(Number(row_number)-1, key, grid_name); 
			break;
		case 'down':
			MarkGridRow(Number(row_number)+1, key, grid_name); 
			break;
	}
}
